﻿using System;
using System.Data.SqlClient;
using Devv.Core.UniDataTier;
using Yglu.BusinessLayer.Entities;

namespace Yglu.BusinessLayer
{
	public partial class DataAccess
	{
		#region Get

		public static SqlDataReader Combo_Get(Int32 id)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@ComboId", id);

			return (SqlDataReader)db.ExecuteReader("upCombo_Get");
		}

		public static SqlDataReader Combo_Get(Order order)
		{
			DataHandler db = new DataHandler();

			db.AddParameter("@OrderId", order.Id);

			return (SqlDataReader)db.ExecuteReader("upCombo_Get");
		}

		#endregion

		#region Insert and update

		public static void Combo_Upsert(Combo combo)
		{
			using (DataHandler db = new DataHandler())
			{
				db.AddParameter("@Title", combo.Title, true);
				db.AddParameter("@Active", combo.Active);
				db.AddParameter("@ProductIds", combo.Products.GetIds());

				if (combo.Id > 0)
				{
					db.AddParameter("@ComboId", combo.Id);
                    db.ExecuteScalar("upCombo_Update");
				}
				else
				{
					combo.Id = Convert.ToInt32(db.ExecuteScalar("upCombo_Insert"));
				}
			}
		}

		#endregion
	}
}
